part of 'cell.dart';

@deprecated
class LabelEditCell extends StatefulWidget {
  final String? hint;
  final String? unit;
  final TextStyle valueStyle;
  final TextStyle hintStyle;
  final TextStyle unitStyle;
  final int? maxLength;
  final Widget? title;
  final Widget? rightWidget;
  final bool? isShowBottomLine;
  final double? verticalSpacing;
  final double? horizontalSpacing;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final GestureTapCallback? onTap;
  final Color? background;
  final int? maxLines;
  final int? minLines;
  final List<TextInputFormatter>? formatters;
  final TextInputType? keyboardType;
  final ValueChanged<String>? onChanged;
  final TextEditingController? controller;
  final bool isShowCount;
  final BoxDecoration? inputDecoration;
  final EdgeInsetsGeometry? inputPadding;
  final bool enable;

  LabelEditCell(
      {super.key,
      String title = "",
      TextStyle titleStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      bool isMust = false,
      this.controller,
      this.hint,
      this.unit,
      this.valueStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      this.hintStyle = const TextStyle(fontSize: 16.0, color: Colors.blueGrey),
      this.unitStyle = const TextStyle(fontSize: 16.0, color: Colors.grey),
      this.maxLength = 20,
      this.rightWidget,
      this.isShowBottomLine,
      this.verticalSpacing,
      this.horizontalSpacing,
      this.padding,
      this.margin,
      this.onTap,
      this.background,
      this.maxLines,
      this.minLines,
      this.formatters,
      this.keyboardType,
      this.inputDecoration,
      this.isShowCount = false,
      this.onChanged,
      this.enable = true,
      this.inputPadding})
      : this.title = Text.rich(
          TextSpan(text: title, style: titleStyle, children: [
            TextSpan(
              text: isMust ? ' *' : '',
              style: titleStyle.copyWith(color: Colors.red),
            )
          ]),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        );

  LabelEditCell.text(
      {super.key,
      String title = "",
      TextStyle titleStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      bool isMust = false,
      String text = '',
      this.hint,
      this.unit,
      this.valueStyle = const TextStyle(fontSize: 16.0, color: Colors.black),
      this.hintStyle = const TextStyle(fontSize: 16.0, color: Colors.blueGrey),
      this.unitStyle = const TextStyle(fontSize: 16.0, color: Colors.grey),
      this.maxLength = 20,
      this.rightWidget,
      this.isShowBottomLine,
      this.verticalSpacing,
      this.horizontalSpacing,
      this.padding,
      this.margin,
      this.onTap,
      this.background,
      this.maxLines,
      this.minLines,
      this.formatters,
      this.keyboardType,
      this.inputDecoration,
      this.isShowCount = false,
      this.onChanged,
      this.enable = true,
      this.inputPadding})
      : this.title = Text.rich(
          TextSpan(text: title, style: titleStyle, children: [
            TextSpan(
              text: isMust ? ' *' : '',
              style: titleStyle.copyWith(color: Colors.red),
            )
          ]),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        this.controller = TextEditingController(text: text);

  @override
  State<StatefulWidget> createState() {
    return LabelEditCellState();
  }
}

class LabelEditCellState extends State<LabelEditCell> {
  late TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = widget.controller ?? TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return LabelCell(
      enable: widget.enable,
      title: Row(
        children: [
          Expanded(child: widget.title ?? Container()),
          widget.maxLength != null && widget.isShowCount
              ? Text.rich(
                  TextSpan(children: [
                    TextSpan(
                        text: '${controller.text.length}',
                        style: TextStyle(color: controller.text.length > widget.maxLength! ? Colors.red : Colors.grey)),
                    TextSpan(text: '/${widget.maxLength}')
                  ]),
                  style: const TextStyle(fontSize: 12, color: Colors.grey),
                )
              : Gaps.empty,
        ],
      ),
      subTitle: Row(
        children: [
          Expanded(
            child: CupertinoTextField(
                cursorHeight: 16.0,
                maxLines: widget.maxLines ?? widget.minLines ?? 1,
                minLines: widget.minLines ?? 1,
                maxLength: widget.maxLength,
                padding: widget.inputPadding ?? const EdgeInsets.all(0.0),
                controller: controller,
                style: widget.valueStyle,
                placeholder: widget.hint ?? '请输入',
                placeholderStyle: widget.hintStyle,
                decoration: widget.inputDecoration ?? const BoxDecoration(),
                keyboardType: widget.keyboardType ?? TextInputType.text,
                inputFormatters: [TextInputLengthLimitingFormatter(widget.maxLength!), ...?widget.formatters],
                onChanged: (str) {
                  widget.onChanged?.call(str);
                  if (mounted) {
                    setState(() {});
                  }
                }),
          ),
          Container(
              alignment: Alignment.bottomRight,
              child: Text(
                widget.unit ?? '',
                style: widget.unitStyle,
              ))
        ],
      ),
      isShowBottomLine: widget.isShowBottomLine ?? false,
      verticalSpacing: widget.verticalSpacing ?? 0,
      horizontalSpacing: widget.horizontalSpacing ?? 0,
      padding: widget.padding,
      margin: widget.margin,
      onTap: widget.onTap,
      background: widget.background,
    );
  }
}
